package server;

import control.CControl;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SFormulaConnectionPanel extends JPanel implements ActionListener, Observer
{
    private CControl cControl;
    
    private JTextField addressTextField, portTextField;
    private JButton connectionSaveButton;
    
    public SFormulaConnectionPanel(CControl cControl)
    {
        super(new GridBagLayout());
        
        initComponent();
        setBackground(new Color(0xC95D5D)); // Red
        
        this.cControl = cControl;
        cControl.addStreamStatusObserver(this);
        
        String ipAddress = cControl.getSettingsParameter("dataaddress");
        String port = cControl.getSettingsParameter("dataport");
        addressTextField.setText(ipAddress);
        portTextField.setText(port);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source == connectionSaveButton)
        {
            String ipAddress = addressTextField.getText();
            String port = portTextField.getText();
                     
            cControl.setSettingsParameter("dataport", port);
            cControl.setSettingsParameter("dataaddress", ipAddress);
            cControl.startStreaming();
        }
    }

    @Override
    public void update(Observable o, Object arg)
    {
        // Check if connection is OK and set background color accordingly
        if (arg instanceof String[])
        {
            String[] status = (String[]) arg;
            switch (status[0])
            {
                case "OK":
                    setBackground(new Color(0xD0EB87)); // Green
                    break;
                case "NC":
                    setBackground(new Color(0xC95D5D)); // Red
                    break;
            }
		}
    }
    
    private void initComponent()
    {
        addressTextField = new JTextField();
        portTextField = new JTextField();
        connectionSaveButton = new JButton("Save & Reconnect");
        connectionSaveButton.addActionListener(this);
        
        GridBagConstraints c = new GridBagConstraints();
        
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 3;
        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(new JLabel("Formula car"), c);
        
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.ipady = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.1;
        JLabel label = new JLabel("IP");
        label.setHorizontalAlignment(JLabel.RIGHT);
        add(label, c);
        c.gridy = 2;
        label = new JLabel("Port");
        label.setHorizontalAlignment(JLabel.RIGHT);
        add(label, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.ipady = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.6;
        add(addressTextField, c);
        c.gridy = 2;
        add(portTextField, c);
        
        c.gridx = 2;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 2;
        c.ipady = 0;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 0.3;
        add(connectionSaveButton, c);
    }
}
